$(function() {
	for(var i = 0; i < $(".filter-nav").length; i++) {
		(function(index) { //点击出现对应的列表
			$(".filter-nav").eq(index).click(function() {
				var claname = $(".filter-extend").eq(index)[0].className;
				if(claname.indexOf("open") == -1) {
					$(".filter-extend").removeClass("open");
					$(".filter-extend").eq(index).addClass("open");
					$(".filter-modal").addClass("open");
					$("body").addClass("hidden");
				} else {
					$(".filter-modal").click();
				}
			})
		})(i)
	}
	$(".filter-modal").click(function() {
		$(".filter-extend").removeClass("open");
		$(".filter-modal").removeClass("open");
		$("body").removeClass("hidden");
	})
})
new Vue({
	el: "body",
	data: {
		shop: [],
		listle: [{
			img: "//fuss10.elemecdn.com/b/ff/533cf9617bd57fe1dfb05603bebcfpng.png",
			name: "快餐便当",
			num: 322,
			hidden: true

		}, {
			img: "//fuss10.elemecdn.com/6/55/ac1bfd1e818013a9f099e964f1e9djpeg.jpeg",
			name: "特色菜系",
			num: 32,
			hidden: false
		}, {
			img: "//fuss10.elemecdn.com/1/ba/bf6efbfdb0ef701f19689a5529e5fjpeg.jpeg",
			name: "异国料理",
			num: 22,
			hidden: false
		}],
		listri: [
			[{
				name: "简餐",
				num: 122
			}, {
				name: "盖浇饭",
				num: 12
			}, {
				name: "麻辣烫",
				num: 22
			}, {
				name: "米粉面馆",
				num: 21
			}, {
				name: "黄焖鸡",
				num: 12
			}, {
				name: "汉堡",
				num: 11
			}, {
				name: "煲仔饭",
				num: 12
			}],
			[{
				name: "东北菜",
				num: 122
			}, {
				name: "江浙菜",
				num: 12
			}, {
				name: "粤菜",
				num: 22
			}, {
				name: "西北菜",
				num: 21
			}, {
				name: "川湘菜",
				num: 12
			}, {
				name: "鲁菜",
				num: 11
			}],
			[{
				name: "日韩料理",
				num: 122
			}, {
				name: "西餐",
				num: 12
			}, {
				name: "意面披萨",
				num: 22
			}, {
				name: "东南亚菜",
				num: 21
			}]
		]

	},
	methods: {
		targetShopmain: function(index) {
			var newUrl = "?shopid=" + this.shop[index].id
			window.location.href = "../shopmain/shopmain.html" + newUrl;
		},
		cli1: function(index) { //点击左侧
			for(var i in this.listle) {
				this.listle[i].hidden = false;
			}
			this.listle[index].hidden = true;
		},
		select: function(num1, num2) {
			this.shop = [];
			var name = this.listri[num1][num2].name;
			console.log(name);
			var this_shop = this.shop;
			var this_id;
			var this_img;
			var this_name;
			var this_ratewarp;
			var this_moneylimit;
			var this_sendprice;
			var this_grade;

			var query = new AV.Query('ELM_shop');
			var tar = query.contains("classify", name); //获取类别关键词
			//				query.include('owner');
			//				query.include('image');
			tar.find().then(function(shoplist) {
				console.log(shoplist)
				// 查询到商品后，在前端展示到相应的位置中
				for(var i = 0; i < shoplist.length; i++) {
					this_id = shoplist[i].id;
					this_img = shoplist[i].attributes.shopimg;
					this_name = shoplist[i].attributes.shopname;
					this_rateWarp = shoplist[i].attributes.salenum;
					this_moneylimit = shoplist[i].attributes.startprice;
					this_sendprice = shoplist[i].attributes.sendprice;
					this_grade = shoplist[i].attributes.grade;
					var grade = parseInt(this_grade);
					var thisGrade = [];
					for(var j = 0; j < grade; j++) {
						thisGrade.push(j);
					}
					var shop = {
						id: this_id,
						img: this_img,
						name: this_name,
						ratewarp: this_rateWarp,
						moneylimit: this_moneylimit,
						sendprice: this_sendprice,
						grade: thisGrade,
					}
					this_shop.push(shop);
				}
				$(".filter-modal").click();
			}).catch(function(error) {
				console.log(error);
			});
		},
		startprice: function() {
			var arr = this.shop;
			for(var i = 0; i < arr.length-1; i++) {
				for(var j = i + 1; j < arr.length; j++)
				if(arr[i].moneylimit > arr[j].moneylimit) {
					console.log(arr[j].moneylimit);
					var temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
			console.log(arr);
			this.shop = arr;
			for(var k=0;k<this.shop.length;k++){
				console.log(this.shop[k].moneylimit);
			}
		},
		grade: function() {
			var arr = this.shop;
			for(var i = 0; i < arr.length-1; i++) {
				for(var j = i + 1; j < arr.length; j++)
				if(arr[i].grade.length > arr[j].grade.length) {
					var temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
			this.shop = arr;
		},
		ratewarp: function() {
			var arr = this.shop;
			for(var i = 0; i < arr.length-1; i++) {
				for(var j = i + 1; j < arr.length; j++)
				if(arr[i].ratewarp > arr[j].ratewarp) {
					var temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
			this.shop = arr;
		}
	},
	created: function() {
		init();
		var sear = get(geturlitem(location.search, "mainWord"));
		var this_shop = this.shop;
		var this_id;
		var this_img;
		var this_name;
		var this_ratewarp;
		var this_moneylimit;
		var this_sendprice;
		var this_grade;

		var query = new AV.Query('ELM_shop');
		var tar = query.contains("classify", sear); //获取类别关键词
		//				query.include('owner');
		//				query.include('image');
		tar.find().then(function(shoplist) {
			console.log(shoplist)
			// 查询到商品后，在前端展示到相应的位置中
			for(var i = 0; i < shoplist.length; i++) {
				this_id = shoplist[i].id;
				this_img = shoplist[i].attributes.shopimg;
				this_name = shoplist[i].attributes.shopname;
				this_rateWarp = shoplist[i].attributes.salenum;
				this_moneylimit = shoplist[i].attributes.startprice;
				this_sendprice = shoplist[i].attributes.sendprice;
				this_grade = shoplist[i].attributes.grade;
				var grade = parseInt(this_grade);
				var thisGrade = [];
				for(var j = 0; j < grade; j++) {
					thisGrade.push(j);
				}

				var shop = {
					id: this_id,
					img: this_img,
					name: this_name,
					ratewarp: this_rateWarp,
					moneylimit: this_moneylimit,
					sendprice: this_sendprice,
					grade: thisGrade,
				}
				this_shop.push(shop);
			}

		}).catch(function(error) {
			console.log(error);
		});

	},
	beforeCompile: function() {
		this.loading = true;
	}
})